package com.lianzhihui.romeex.config;

import android.content.Intent;

import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.bean.user.UserInfo;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.ui.base.ActivityManager;
import com.lianzhihui.romeex.utils.PreferenceUtils;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * Copyright (C), 2019-2019, wase_five
 * FileName: DataBaseManager
 * Author: echo
 * Date: 2019/10/28 17:37
 * Description: 数据库管理
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
public class DataManager {

    private static WalletInfo walletInfo = new WalletInfo();

    public static WalletInfo getUserInfo() {
        if (walletInfo == null) {
            restartApplication();
        }
        return walletInfo;
    }

    public static void setUserInfo(WalletInfo w) {
        walletInfo = w;
    }

    public static void setAuthorization(String token) {
        PreferenceUtils.applyString(AppConstants.Share.AUTHORIZATION, token);
    }

    public static String getAuthorization() {
        return PreferenceUtils.getString(AppConstants.Share.AUTHORIZATION, "");
    }

    /**
     * 报错则重启应用
     */
    public static void restartApplication() {
        ActivityManager.getInstance().finishAllActivity();
        final Intent intent = MyApplication.mContext.getPackageManager().getLaunchIntentForPackage(MyApplication.mContext.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

}
